import 'package:blue_classic_plus/BluetoothDevice.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:mybase/base/tool/platformUtils/FFPlatform.dart';
import 'package:permission_handler/permission_handler.dart';

import 'blue_classic_plus_platform_interface.dart';

/// An implementation of [BlueClassicPlusPlatform] that uses method channels.
class MethodChannelBlueClassicPlus extends BlueClassicPlusPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('blue_classic_plus');

  @override
  Future<bool> bluetoothPermission() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.location,
      Permission.bluetoothScan,
      Permission.bluetoothConnect,
      Permission.bluetoothAdvertise,
    ].request();

    bool allEnabled = true;
    for (var status in statuses.values) {
      if (status != PermissionStatus.granted) {
        allEnabled = false;
        break;
      }
    }

    if (!allEnabled) {
      return false;
    }

    return true;
  }

  @override
  Future<String?> getPlatformVersion() async {
    final version =
        await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<bool> bluetoothIsSupport() async {
    return await methodChannel.invokeMethod<bool>("bluetoothIsSupport") ??
        false;
  }

  @override
  Future<bool> bluetoothIsEnabled() async {
    return await methodChannel.invokeMethod<bool>("bluetoothIsEnabled") ??
        false;
  }

  @override
  Future<bool> bluetoothEnableEetectability() async {
    return await methodChannel
            .invokeMethod<bool>("bluetoothEnableEetectability") ??
        false;
  }

  @override
  Future<List<Object?>> bluetoothBondedDevices() async {
    return await methodChannel
            .invokeMethod<List<Object?>>("bluetoothBondedDevices") ??
        [];
  }

  @override
  Future<bool> bluetoothStartDiscovery(
      {required String uuid, required String name}) async {
    return await methodChannel.invokeMethod<bool>("bluetoothStartDiscovery", {
          "uuid": uuid,
          "name": name,
        }) ??
        false;
  }

  @override
  Future<void> bluetoothEndDiscovery() async {
    return await methodChannel.invokeMethod("bluetoothEndDiscovery");
  }

  @override
  Future<bool> bluetoothStartServer(
      {required String uuid, required String name}) async {
    return await methodChannel.invokeMethod("bluetoothStartServer", {
      "uuid": uuid,
      "name": name,
    });
  }

  @override
  Future<bool> bluetoothStopServer({String? uuid, String? name}) async {
    return await methodChannel.invokeMethod("bluetoothStopServer");
  }

  @override
  Future<bool> bluetoothStartClient(
      {required String uuid, required BluetoothDevice device}) async {
    return await methodChannel.invokeMethod("bluetoothStartClient", {
      "uuid": uuid,
      "address": device.address,
    });
  }

  @override
  Future<bool> bluetoothStopClient(
      {required String uuid, required String device}) async {
    return await methodChannel.invokeMethod("bluetoothStopClient");
  }

  @override
  Future<bool> bluetoothServerBroadcastMsg({required String msg}) async {
    return await methodChannel
        .invokeMethod("bluetoothServerBroadcastMsg", {"msg": msg});
  }

  @override
  Future<bool> bluetoothClientSendMsg({required String msg}) async {
    return await methodChannel
        .invokeMethod("bluetoothClientSendMsg", {"msg": msg});
  }
}
